package tankWar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class panel extends JPanel  implements KeyListener, ActionListener, MouseListener,Runnable {

    //声明变量
    public static int threadNum=0;
    public static int keyCodeBullet=0;
    int m;
    int x,y;
    int tankX, tankY;
    int bulletX,bulletY;
    int loa[][]=new int[1000][1000];
    Random random=new Random(); //nextInt,50
    String dir,dirBullet;//初始方向
    Timer timer=new Timer(1,this);
    Timer timer01=new Timer(5,this);
    Boolean isStart=false;
    Boolean isFalse=false;

    public panel(){
        //总构造方法
        init_start();
        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);//获得键盘监听事件
        this.addMouseListener(this);
        timer.start();
    }

    public void init_start(){
        //游戏初始界面
        tankX =(random.nextInt(2)*6+5)*50;
        tankY =17*50;
        bulletX=tankX;
        bulletY=tankY;
        dir="U";

        this.setLayout(null);
    }


    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);//清屏
        this.setBackground(new Color(29, 29, 29));
        //绘制初始界面
        if (isStart){
            for (int i=1;;){
                for (int j=2;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=3;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=5;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=7;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=9;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=11;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=13;;){
                for (int j=1;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=15;;){
                for (int j=2;j<=11;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }

            for (int i=2;;){
                for (int j=3;j<=9;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=14;;){
                for (int j=3;j<=9;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int j=12;;){
                for (int i=3;i<=13;i++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=1;i<=3;i++){
                for (int j=14;j<=16;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
            }
            for (int i=13;i<=15;i++){
                for (int j=14;j<=16;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
            }
//        data.will02.paintIcon(this,g,100,850);
            for (int i=6;;){
                for (int j=14;j<=17;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int i=10;;){
                for (int j=14;j<=17;j++){
                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            for (int j=14;;){
                for (int i=6;i<=10;i++){

                    data.will02.paintIcon(this,g,i*50,j*50);
                    loa[i][j]=1;
                }
                break;
            }
            if (dir.equals("U")){
                data.tankU.paintIcon(this,g, tankX, tankY);
            }else if (dir.equals("D")){
                data.tankD.paintIcon(this,g, tankX, tankY);
            }else if (dir.equals("L")){
                data.tankL.paintIcon(this,g, tankX, tankY);
            }else if (dir.equals("R")){
                data.tankR.paintIcon(this,g, tankX, tankY);
            }

            data.bullet01.paintIcon(this,g, bulletX,bulletY);
//            System.out.println(loa[150][150]);
//            if (loa[bulletX][bulletY]==1){
//                data.tankR.paintIcon(this,g,bulletX,bulletY);
//            }

        }
        if (!isStart){
            data.title.paintIcon(this,g,250,150);
            g.setColor(new Color(255, 72, 0));
            g.setFont(new Font("宋体",Font.BOLD,50));
            g.drawString("空格开始游戏",380,550);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFalse){
            if (dir.equals("U")&& tankY !=40){
                tankY = tankY -1;
                bulletY-=4;
            }else if (dir.equals("D")&& tankY !=845){
                tankY = tankY +1;
                bulletY+=4;
            }else if (dir.equals("L")&& tankX !=0){
                tankX -=1;
                bulletX-=4;
            }else if (dir.equals("R")&& tankX !=800){
                tankX +=1;
                bulletY+=4;
            }

            repaint();
        }
        timer.start();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode=e.getKeyCode();

        //不能回头吃自己
        if (dir=="R"&&(keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A)){
            dir="R";
        }else if (dir=="L"&&(keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D)){
            dir="L";
        }else if (dir=="U"&&(keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S)){
            dir="U";
        }else if (dir=="D"&&(keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W)){
            dir="D";
        }else if (dir=="FJY"){

        }
        else{
            if (keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A){
                dir = "L";
            }else if (keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D){
                dir = "R";
            }else if (keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W){
                dir = "U";
            }else if (keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S){
                dir = "D";
            }
        }

        if (keyCode==KeyEvent.VK_SPACE){
            isStart=!isStart;
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {
//        int mouse= e.;
//        System.out.println(mouse);
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void run() {
        x=1;
    }
}

//        jButton_One.setBounds(300,450,300,80);
//        jButton_One.setBackground(new Color(29, 29, 29));
//        jButton_One.setBorderPainted(false);
//        jButton_One.setFont(new Font("微软雅黑",Font.ITALIC,30));
//        jButton_One.setForeground(new Color(255, 187, 28, 190));
//
//        jButton_Two.setBounds(300,550,300,80);
//        jButton_Two.setBackground(new Color(29, 29, 29));
//        jButton_Two.setBorderPainted(false);
//        jButton_Two.setFont(new Font("微软雅黑",Font.ITALIC,30));
//        jButton_Two.setForeground(new Color(255, 187, 28, 190));
//
//        jButton_Exit.setBounds(300,650,300,80);
//        jButton_Exit.setBackground(new Color(29, 29, 29));
//        jButton_Exit.setBorderPainted(false);
//        jButton_Exit.setFont(new Font("微软雅黑",Font.ITALIC,30));
//        jButton_Exit.setForeground(new Color(255, 187, 28, 190));
//
//        this.add(jButton_One);
//        this.add(jButton_Two);
//        this.add(jButton_Exit);

/*
    JButton jButton_One = new JButton("Single Mode");
    JButton jButton_Two = new JButton("Double Mode");
    JButton jButton_Exit = new JButton("Exit");
 */